package com.zxl.yunpicture.domain.user.entity;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zxl.yunpicture.domain.user.valueObject.UserRoleEnum;
import com.zxl.yunpicture.infrastructure.exception.BusinessException;
import com.zxl.yunpicture.infrastructure.exception.ErrorCode;
import com.zxl.yunpicture.interfaces.dto.user.UserLoginRequest;
import com.zxl.yunpicture.interfaces.dto.user.UserRegisterRequest;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * 用户
 * @TableName user
 */
@TableName(value ="user")
@Data
public class User implements Serializable {
    /**
     * id
     * type = IdType.ASSIGN_ID 长字段id
     */
    @TableId(type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 账号
     */
    private String userAccount;

    /**
     * 密码
     */
    private String userPassword;

    /**
     * 用户昵称
     */
    private String userName;

    /**
     * 用户头像
     */
    private String userAvatar;

    /**
     * 用户简介
     */
    private String userProfile;

    /**
     * 用户角色：user/admin
     */
    private String userRole;

    /**
     * 编辑时间
     */
    private Date editTime;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 是否删除
     */
    @TableLogic
    private Integer isDelete;

    private static final long serialVersionUID = 1L;


    /**
     * 校验用户注册
     *
     * @param userRegisterRequest
     */
    public static void validUserRegister(UserRegisterRequest userRegisterRequest){
        //1. 效验参数
        if (StrUtil.hasBlank(userRegisterRequest.getUserAccount(),userRegisterRequest.getUserPassword(),userRegisterRequest.getCheckPassword())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        if (userRegisterRequest.getUserAccount().length() < 4){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户账户过短");
        }
        if (userRegisterRequest.getUserPassword().length() < 8 || userRegisterRequest.getCheckPassword().length() < 8){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户密码过短");
        }
        if (!userRegisterRequest.getUserPassword().equals(userRegisterRequest.getCheckPassword())) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "两次输入的密码不一致");
        }
    }


    /**
     * 校验用户登录
     *
     * @param userLoginRequest
     */
    public static void validUserLogin(UserLoginRequest userLoginRequest) {
        //1.校验
        if (StrUtil.hasBlank(userLoginRequest.getUserAccount(),userLoginRequest.getUserPassword())){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        if (userLoginRequest.getUserAccount().length() < 4){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"账号错误");
        }
        if (userLoginRequest.getUserPassword().length() < 8){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"密码错误");
        }
    }


    /**
     * 是否为管理员
     * @return
     */
    public boolean isAdmin() {
        //用户不能为空 且用户的userRole属性需为admin 才返回true
        return UserRoleEnum.ADMIN.getValue().equals(this.getUserRole());
    }
}